﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DevExpress.MailClient.Win.TasksManager
{
    public partial class TasksManagerViewer : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {
        public enum WORKFLOW_WINDOW_TYPES
        {
            N_A = 0,
            TASK_MANAGER,
            MY_REQUESTS
        }

        public enum WORKFLOW_ACTIONS
        {
            N_A = 0,
            APPROVE,
            REJECT
        }

        public WORKFLOW_WINDOW_TYPES _cURRENT_SELECTED_WORKFLOW_WINDOW_TYPE;
        public WORKFLOW_WINDOW_TYPES CURRENT_SELECTED_WORKFLOW_WINDOW_TYPE
        {
            get { return _cURRENT_SELECTED_WORKFLOW_WINDOW_TYPE; }
            set
            {
                PRO_DOCSDatabaseDataContext = null;
                
                _cURRENT_SELECTED_WORKFLOW_WINDOW_TYPE = value;
                
                List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_WORKFLOW> aRCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCES = new List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_WORKFLOW>();

                switch (_cURRENT_SELECTED_WORKFLOW_WINDOW_TYPE)
                {
                    case WORKFLOW_WINDOW_TYPES.N_A:
                        break;
                    case WORKFLOW_WINDOW_TYPES.TASK_MANAGER:
                        {

                            List<PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENT_FILE_WORKFLOWSLoadAllByTheStepUserResult> current_user_assigning = PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_WORKFLOWSLoadAllByTheStepUser(PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID).ToList();
                            
                            for (int i = 0; i < current_user_assigning.Count; i++)
                            {
                                aRCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCES.Add
                                (
                                    (
                                        from workflows in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_WORKFLOWs
                                        where workflows.APPLICATION_WORKFLOW_INSTANCE_ID.Equals
                                        (
                                            current_user_assigning[i].APPLICATION_WORKFLOW_INSTANCE_ID
                                        )
                                        select workflows
                                    ).FirstOrDefault()
                                );
                            }
                            ucWorkFlowsListDisplay1.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCES = aRCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCES;
                        }
                        break;
                    case WORKFLOW_WINDOW_TYPES.MY_REQUESTS:
                        {

                            List<PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENT_FILE_WORKFLOWSLoadAllByTheCurrentUserResult> current_requests_by_user = PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_WORKFLOWSLoadAllByTheCurrentUser(PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID).ToList();

                            for (int i = 0; i < current_requests_by_user.Count; i++)
                            {
                                aRCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCES.Add
                                (
                                    (
                                        from workflows in PRO_DOCSDatabaseDataContext.ARCHIVE_DEPARTMENT_FILE_WORKFLOWs
                                        where workflows.APPLICATION_WORKFLOW_INSTANCE_ID.Equals
                                        (
                                            current_requests_by_user[i].APPLICATION_WORKFLOW_INSTANCE_ID
                                        )
                                        select workflows
                                    ).FirstOrDefault()
                                );
                            }
                            ucWorkFlowsListDisplay1.ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCES = aRCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCES;

                        }
                        break;
                    default:
                        break;
                }
            }
        }

        public TasksManagerViewer(WORKFLOW_WINDOW_TYPES cURRENT_SELECTED_WORKFLOW_WINDOW_TYPE)
        {
            InitializeComponent();
            CURRENT_SELECTED_WORKFLOW_WINDOW_TYPE = cURRENT_SELECTED_WORKFLOW_WINDOW_TYPE;
        }

        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY ARCHIVE_DEPARTMENT_FILE_HISTORY
        { get; set; }

        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_WORKFLOW _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE;
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_WORKFLOW CURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE
        {
            get { return _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE; }
            set
            {
                _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE = value;

                List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY> aRCHIVE_DEPARTMENT_FILE_HISTORies = _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE.ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_HISTORies.ToList();
                aRCHIVE_DEPARTMENT_FILE_HISTORies = aRCHIVE_DEPARTMENT_FILE_HISTORies.OrderBy(k => k.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE.Value).ToList();
                ARCHIVE_DEPARTMENT_FILE_HISTORY = aRCHIVE_DEPARTMENT_FILE_HISTORies[aRCHIVE_DEPARTMENT_FILE_HISTORies.Count - 1];

                //LOAD the Physical file viewer
                ucWorkFlowInformatinViewer1.ucFilePhysicalViewer1.CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = ARCHIVE_DEPARTMENT_FILE_HISTORY;
                //LOAD dynamic indecies

                ucWorkFlowInformatinViewer1.ucFileDynamicIndeciesDetails1.CURRENT_ARCHIVE_DEPARTMENT_FILE = _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE.ARCHIVE_DEPARTMENT_FILE;
                ucWorkFlowInformatinViewer1.ucFileDynamicIndeciesDetails1.CURRENT_ARCHIVE_DEPARTMENT_FILE_HISTORY = ARCHIVE_DEPARTMENT_FILE_HISTORY;
                //ucWorkFlowInformatinViewer1.ucFileDynamicIndeciesDetails1.SET_FILE_INDECIES();
                //the instance Details
                ucWorkFlowInformatinViewer1.DISPLAY_FILE_WORKFLOW_INFOEMATION(_cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE);
                //display the workflow steps
                ucWorkFlowInformatinViewer1.ucWorkFlowStepsDisplay1.CURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW = _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE;

            }
        }

        internal void SET_STEP_ACTION(WORKFLOW_ACTIONS wORKFLOW_ACTION)
        {
            if (ucWorkFlowInformatinViewer1.SET_FILE_WORKFLOW_ACTION(wORKFLOW_ACTION, _cURRENT_ARCHIVE_DEPARTMENT_FILE_WORKFLOW_INSTANCE))
            {
                CURRENT_SELECTED_WORKFLOW_WINDOW_TYPE = CURRENT_SELECTED_WORKFLOW_WINDOW_TYPE;
            }
        }
    }
}